package rsj.service;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.commons.lang3.StringUtils;

import com.rlyy.aibase.utils.base.MapNode;

import common.util.Assert;
import common.util.Md5Util;
import rsj.dao.OrgDao;
import rsj.dao.UserInfoDao;
import rsj.db.dbutils.UniqueID;
import rsj.manager.OrgProManager;

public class OrgService {
	
	private static final OrgService instance= new OrgService();
	
	public static OrgService getInstance(){
		return instance;
	}
	
	private static OrgDao infoDao = OrgDao.getInstance();
	private static UserInfoDao userDao = UserInfoDao.getInstance();
	private static OrgProManager orgProManager = OrgProManager.getInstance();
	
	public MapNode findOrgList(Connection conn,MapNode data) throws SQLException{
		int size = data.getValueIntDef("Page_Size", 10);
		int pcount = data.getValueIntDef("Page_Count", 1);
		String querykey = data.getValue("Query_Key");
		return infoDao.findOrgList(conn, size, pcount, querykey);
	}
	
	public void orgInsert(Connection conn, MapNode data, String tid) throws SQLException{
		String id = String.valueOf(UniqueID.generateId());
		String orgName = data.getValue("Org_Name");
	   	String orgSname = data.getValue("Org_Sname");	   	
	   	String creditcode = data.getValue("Org_Creditcode");
	   	String legname = data.getValue("Org_Legname");
	   	String addr = data.getValue("Org_Addr");
	   	String manoper = data.getValue("Org_Manoper");
	   	String manoperphone = data.getValue("Org_Manoperphone");
	   	String manopermail = data.getValue("Org_Manopermail");
	   	String mantech = data.getValue("Org_Mantech");
	   	String mantechphone = data.getValue("Org_Mantechphone");	   	
	   	String mantechmail = data.getValue("Org_Mantechmail");
	   	String orgIp = data.getValue("Org_Ip");
	   	String manbus = data.getValue("Org_Manbus");
	   	String manbusphone = data.getValue("Org_Manbusphone");
	   	String manbusmail = data.getValue("Org_Manbusmail");
	   	String orgUserName = data.getValue("Org_User_Name");
	   	String password = data.getValue("Org_User_Password");
	   	
	   	Assert.isNotBlank(orgName,"客户名称不能为空");
    	Assert.isNotBlank(orgSname,"客户简称不能为空");
    	Assert.isNotBlank(creditcode,"统一社会信用代码不能为空");
    	Assert.isNotBlank(legname,"法人名称不能为空");
    	Assert.isNotBlank(addr,"地址不能为空");
    	Assert.isNotBlank(manoper,"运营负责人不能为空");
    	Assert.isNotBlank(manoperphone,"运营负责人手机号不能为空");
    	Assert.isNotBlank(manopermail,"运营负责人邮箱不能为空");
    	Assert.isNotBlank(mantech,"技术负责人不能为空");
    	Assert.isNotBlank(mantechphone,"技术负责人手机号不能为空");
    	Assert.isNotBlank(mantechmail,"技术负责人邮箱不能为空");
    	Assert.isNotBlank(orgIp,"ip地址不能为空");
    	Assert.isNotBlank(manbus,"商务负责人不能为空");
    	Assert.isNotBlank(manbusphone,"商务负责人手机号不能为空");
    	Assert.isNotBlank(manbusmail,"商务负责人邮箱不能为空");
    	Assert.isNotBlank(orgUserName,"客户Id不能为空");
    	Assert.isNotBlank(password,"客户密码不能为空");
    	
    	infoDao.orgInsert(conn, id, orgName, orgSname, creditcode, legname, addr, manoper, manoperphone, manopermail, mantech, mantechphone, mantechmail, manbus, manbusphone, manbusmail, orgIp, orgUserName,password);

		orgProManager.createAuth(conn, id, tid);
	}
	
	public void orgUpdate(Connection con,MapNode data) throws SQLException{
		
		String orgName = data.getValue("Org_Name");
	   	String orgSname = data.getValue("Org_Sname");	   	
	   	String creditcode = data.getValue("Org_Creditcode");
	   	String legname = data.getValue("Org_Legname");
	   	String addr = data.getValue("Org_Addr");
	   	String manoper = data.getValue("Org_Manoper");
	   	String manoperphone = data.getValue("Org_Manoperphone");
	   	String manopermail = data.getValue("Org_Manopermail");
	   	String mantech = data.getValue("Org_Mantech");
	   	String mantechphone = data.getValue("Org_Mantechphone");	   	
	   	String mantechmail = data.getValue("Org_Mantechmail");
	   	String orgIp = data.getValue("Org_Ip");
	   	String manbus = data.getValue("Org_Manbus");
	   	String manbusphone = data.getValue("Org_Manbusphone");
	   	String manbusmail = data.getValue("Org_Manbusmail");
	   	String orgId = data.getValue("Org_Id");
		String password = data.getValue("Org_User_Password");
		String orgUserName = data.getValue("Org_User_Name");
	   	
	   	
		Assert.isNotBlank(orgName,"客户名称不能为空");
    	Assert.isNotBlank(orgSname,"客户简称不能为空");
    	Assert.isNotBlank(creditcode,"统一社会信用代码不能为空");
    	Assert.isNotBlank(legname,"法人名称不能为空");
    	Assert.isNotBlank(addr,"地址不能为空");
    	Assert.isNotBlank(manoper,"运营负责人不能为空");
    	Assert.isNotBlank(manoperphone,"运营负责人手机号不能为空");
    	Assert.isNotBlank(manopermail,"运营负责人邮箱不能为空");
    	Assert.isNotBlank(mantech,"技术负责人不能为空");
    	Assert.isNotBlank(mantechphone,"技术负责人手机号不能为空");
    	Assert.isNotBlank(mantechmail,"技术负责人邮箱不能为空");
    	Assert.isNotBlank(orgIp,"ip地址不能为空");
    	Assert.isNotBlank(manbus,"商务负责人不能为空");
    	Assert.isNotBlank(manbusphone,"商务负责人手机号不能为空");
    	Assert.isNotBlank(manbusmail,"商务负责人邮箱不能为空");
    	Assert.isNotBlank(orgId,"机构Id不能为空");
    
	   	
	   	infoDao.orgUpdate(con, orgId, orgName, orgSname, creditcode, legname, addr, manoper,
				manoperphone, manopermail, mantech, mantechphone, mantechmail, manbus, manbusphone, manbusmail, orgIp);
	   	//机构简称修改，要把用户表的姓名也修改
	   	userDao.updateUserName(con, orgUserName, orgSname);
	   	//修改该机构所对应的用户的密码
	   	if(StringUtils.isNotBlank(password)){
	   		userDao.updatePassword(con, orgUserName, Md5Util.stringToMD5(password));
	   	}
	   	
	}
	
	public MapNode findOrgInfoById(Connection con,MapNode data) throws SQLException{
		String orgId = data.getValue("Org_Id");
		Assert.isNotBlank(orgId,"机构Id不能为空");
		
		return infoDao.findOrgInfoById(con, orgId);
	}
	
	public MapNode findUserOrgInfo(Connection con,MapNode data) throws SQLException{
		String userId = data.getValue("User_Id");
		Assert.isNotBlank(userId,"用户Id不能为空");
		
		return infoDao.findUserOrgInfo(con, userId);
	}

}
